package com.mnnu.controller;


import com.mnnu.pojo.Result;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class SessionController {

    @GetMapping("/c1")
    public Result setCookie(HttpServletResponse response) {
        response.addCookie(new Cookie("username", "dockanddogg")); //设置cookie

        return Result.success();
    }

    @GetMapping("/c2")
    public Result Cookie(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();  //获取所有cookie

        for(Cookie cookie : cookies) {
            if(cookie.getName().equals("dockanddogg")) {
                System.out.println("username:" + cookie.getValue());
            }
        }

        return Result.success();
    }
}
